<?php

require_once(APPPATH.'libraries/MY_PortalController.php');

class Process_cron_info extends MY_PortalController{

	var $rows=500;

	function Process_cron_info(){
		parent::MY_PortalController();
	}

	//abstract
	protected function process_response($response,$events){}
	//end-abstract

	protected function run($ticket,$events){
		if(CONTROLLER_CRON!==1){
			exit('No direct script access allowed');
		}

		echo date("d/m/Y G:i:s") . " - Running cron $ticket...\n";

		$this->load->model('m_conf_param');
		$ticket_param = $this->m_conf_param->getById($ticket);
		//Si no existe se crea
		if($ticket_param==NULL){
			$ticket_param = new ConfParam();
			$ticket_param->name=$ticket;
			$ticket_param->value="0";
			$this->m_conf_param->create($ticket_param);
		}

		$date_now = date_create();
		$ok = true;
		try{
			$this->load_api();
			do{
				$response = $this->api->get_backend_events((int)$ticket_param->value,$this->rows,$events);
				$ticket_param->value = $response["next_ticket"];
				$this->process_response($response);
			}while($response['num_rows']>=$this->rows);
		}catch(Exception $ex){
			$ok = false;
			echo "Error:".$ex->getMessage()."\n";
		}
		if($ok){
			$this->m_conf_param->update($ticket_param);
		}
	}

}
?>

